package com.rf.richfitwheel.admin.sys.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.CodeDefMapper;
import com.rf.richfitwheel.admin.sys.dao.SqlAdapterMapper;
import com.rf.richfitwheel.admin.sys.model.CodeDef;
import com.rf.richfitwheel.admin.sys.model.CodeDefExample;
import com.rf.richfitwheel.admin.sys.service.CodeDefService;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Author bw
 * Date  2019-05-05
 */
@Slf4j
@Service
public class CodeDefServiceImpl implements CodeDefService {

    @Resource
    private CodeDefMapper codeDefDao;

	@Resource
	private SqlAdapterMapper sqlAdapterMapper;
    
    @Override
    public CodeDef get(String id){
        return codeDefDao.selectByPrimaryKey(id);
    }
    
    @Override
    public List<CodeDef> findList(CodeDef codeDef) {
		CodeDefExample example = new CodeDefExample();
		example.setOrderByClause(" code ");
		return codeDefDao.selectByExample(example);
    }
    
    @Override
    public List<CodeDef> findAllList() {
		CodeDefExample example = new CodeDefExample();
		example.setOrderByClause(" code ");
		return codeDefDao.selectByExample(example);
    }
    
    @Override
    public R insert(CodeDef codeDef) {
    	try {
    		int flag = checkCodeExist(codeDef.getCode());
    		if(flag==1) {
    			return R.error("该编码编号已存在");
    		}
    		String id= UuidUtil.get32UUID();
    		Integer version = 1;
    		codeDef.setId(id);
    		codeDef.setVersion(version);
    		codeDefDao.insert(codeDef);
    		return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
        
    }
    
    @Override
	@Transactional(rollbackFor = Exception.class)
    public int insertBatch(List<CodeDef> codeDefs){
		for(CodeDef codeDef : codeDefs){
			codeDefDao.insertSelective(codeDef);
		}
		return codeDefs.size();
    }
    
    @Override
    public R update(CodeDef codeDef) {
    	try {
			codeDefDao.updateByPrimaryKeySelective(codeDef);
    		return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
         
    }
    
    @Override
    public R delete(String[] ids) {
        try {
			if(ids!=null&&ids.length>0) {
				for (String id : ids) {
					CodeDef cd = new CodeDef();
					cd.setId(id);
					codeDefDao.deleteByPrimaryKey(id);
				}
			}
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
    }

    //查找分页列表
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		try {

			PageUtils pageUtils = PageUtils.getPageInfo(params);
			Page<CodeDef> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
			CodeDef codeDef = (CodeDef) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),
					CodeDef.class);
			CodeDefExample example = new CodeDefExample();
			CodeDefExample.Criteria criteria = example.createCriteria();
			if(codeDef != null) {
				if (StringUtils.isNotBlank(codeDef.getCode())) {
					criteria.andCodeLike("%" + codeDef.getCode() + "%");
				}
				if (StringUtils.isNotBlank(codeDef.getName())) {
					criteria.andNameLike("%" + codeDef.getName() + "%");
				}
			}
			example.setOrderByClause(" code ");
			codeDefDao.selectByExample(example);
			/*Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
			StringBuffer sql = new StringBuffer(
					" select c.id,c.code,c.name,c.over,c.des,c.version from sys_code_def c ");
			CodeDef codeDef = (CodeDef) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),
					CodeDef.class);
			sql.append(" where 1=1 ");
			String code = (String) params.get("code");
			if(code != null && !"".equals(code)) {
				code = code.trim();
				sql.append(" and c.code like '%"+code+"%'");
			}
			String name = (String) params.get("name");
			if(name != null && !"".equals(name)) {
				name = name.trim();
				sql.append(" and c.name like '%"+name+"%'");
			}
			
			sqlAdapterMapper.selectSQL(sql.toString());
			pageUtils = new PageUtils(p);
			List<Map<String, String>> mapList = (List<Map<String, String>>) pageUtils.getList();
			
			pageUtils.setList(mapList);*/
			pageUtils = new PageUtils(p);
			return pageUtils;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//新增时编码编号查重
	public int checkCodeExist(String code) {
		try {
			CodeDefExample example = new CodeDefExample();
			CodeDefExample.Criteria criteria = example.createCriteria();
			criteria.andCodeEqualTo(code);
			List<CodeDef> list = codeDefDao.selectByExample(example);
			if(list.isEmpty()) {
				return 0;
			}else {
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
	}

}
